package com.cloudm.technician

import com.cloudm.technician.data.config.Config
import com.cloudm.technician.data.model.Response
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okio.Buffer
import org.junit.AfterClass
import org.junit.BeforeClass

/**
 * 作者: wesley
 * 时间: 2017/11/30
 * 描述:
 */
abstract class BaseMockServer {

    companion object {
        var server: MockWebServer? = null

        @JvmStatic
        @BeforeClass
        fun before() {
            server = MockWebServer()
            server?.start()
            Config.baseUrl = server?.url("/").toString()
        }

        @JvmStatic
        @AfterClass
        fun after() {
            server?.shutdown()
        }
    }

    protected fun <T> mockWebData(t : Response<T?>) {
        val mockResponse = MockResponse().apply {
            setResponseCode(200)
            body = Buffer().write(com.google.gson.Gson().toJson(t).toByteArray())
        }
        server?.enqueue(mockResponse)
    }
}